package com.qyhl.module_home.home.fragment2.main;

import com.orhanobut.hawk.Hawk;
import com.qyhl.webtv.commonlib.entity.home.GatherBean;
import com.qyhl.webtv.commonlib.entity.home.HomeBean;
import com.qyhl.webtv.commonlib.entity.live.ShoppingListBean;
import com.qyhl.webtv.commonlib.entity.news.AdvHomeBean;

import java.util.List;

/**
 * Created by Administrator on 2018/3/20.
 */

public class HomeListPresenterImpl implements HomeListContract.HomeListViewPresenter, HomeListContract.HomeListModelPresenter {
    private HomeListContract.HomeListModel model;
    private HomeListContract.HomeListView homeListView;

    public HomeListPresenterImpl(HomeListContract.HomeListView homeListView) {
        model = new HomeListModelImpl(this);
        this.homeListView = homeListView;
    }

    @Override
    public void getConfig() {
        model.getConfig();
    }

    @Override
    public void getAdvData() {
        model.getAdvData();
    }

    @Override
    public void onDestroy() {
    }

    @Override
    public void getGatherData(int page, int lastNewsId, String type) {
        model.getGatherData(page, lastNewsId, type);
    }

    @Override
    public void getTopShop() {
        model.getTopShop();
    }


    @Override
    public void setError(int code, String msg) {
        if (code == 1) {
            if (Hawk.contains("home")) {
//                HomeBean bean = Hawk.get("home");
//                setConfig(bean);//启用缓存数据
            } else {
                homeListView.showError(msg);
            }
            homeListView.showMessage(msg);
        } else if (code == 2) {
//            if (Hawk.contains("gather")) {
//                JsonGather bean = Hawk.get("gather");
//                setGatherNews(bean, false);//启用缓存数据
//            }
            homeListView.showMessage(msg);
        } else if (code == 3) {
            homeListView.showMessage(msg);
        } else if (code == 4) {
//            if (Hawk.contains("notgather")) {
//                JsonNotGather bean = Hawk.get("notgather");
//                homeListView.setNormalNews(bean.getData(), false);
//            }
            homeListView.showMessage(msg);
        } else if (code == 5) {
            homeListView.showEmpty("无结果！");
        } else {

        }
    }

    @Override
    public void setConfig(HomeBean bean) {
        List<HomeBean.TopNews> topNewsList = bean.getTopNews();
        List<HomeBean.SecMenus> threeMenus = bean.getSecMenus();
        //是否显示轮播
        if (topNewsList != null && topNewsList.size() != 0) {
            homeListView.setBanner(true, topNewsList);
        } else {
            homeListView.setBanner(false, null);
        }

        //是否显示导航
        if (threeMenus != null && threeMenus.size() != 0) {
            homeListView.setNavigation(true, threeMenus);
        } else {
            homeListView.setNavigation(false, null);
        }
    }

    @Override
    public void setAdvBanner(boolean isShow, List<AdvHomeBean> list) {
        if (isShow) {
            homeListView.setAdvBanner(true, list);
        } else {
            homeListView.setAdvBanner(false, null);
        }
    }

    @Override
    public void setGatherNews(List<GatherBean> bean, boolean isMore) {
        try {
            homeListView.setGatherNews(bean, isMore);
        } catch (Exception e) {
            Hawk.delete("home");
            Hawk.delete("gather");
        }
    }

    @Override
    public void setTopShop(List<ShoppingListBean> bean) {
          homeListView.setTopShop(bean);
    }


}
